#!/usr/bin/env bash
#JAVA_HOME="/usr/local/java/jdk1.8.0_261"
#export JAVA_HOME=${JAVA_HOME}
#export PATH=${JAVA_HOME}/bin:$PATH

cd /root/seckill-demo
rm -rf output
mkdir output

echo "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
echo "[INFO]开始更新代码......"
echo "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"

git pull

echo "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
echo "[INFO]开始Maven打包......"
echo "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
mvn clean package -Dmaven.test.skip=true -U
if [ $? -ne 0 ]; then
    echo ""
    echo "***********************************************************"
    echo "[INFO]Maven打包失败"
    echo "***********************************************************"
    exit 1
fi
cp Dockerfile output/Dockerfile
cp target/seckill-demo-0.0.1-SNAPSHOT.jar output/seckill-demo-0.0.1-SNAPSHOT.jar

# remove old ps & images
docker stop seckill-demo
docker rm seckill-demo
docker rmi seckill-demo:0.0.1
echo '>>> old container removed'

echo '================开始构建镜像================'
cd output
docker build -t seckill-demo:0.0.1 .
echo '================结束构建镜像================'

echo '================开始运行新容器================'
docker run -itd -p 8087:8087 --restart always --name=seckill-demo --privileged=true oatest:0.0.1
echo '================运行新容器完毕================'

echo '================结束远程启动================'
